<?php 

Class ModuleOSandBrowserResult extends CmsCustomModule {
	
	private function getData($num){
		$v = array();
		for($i=0; $i<$num; $i++){
			$v[] = rand(100, 10000);
		}
		return $v;
	}
	private function getItemHTML($list){
		$datas = '';
		if(rand(0, 3)){
			$os_v = $this->getData(4);
			$os_capture = json_encode( array_values(AC::$os) );
			$browsers_v = $this->getData(3);
			$browsers_capture = json_encode( array_values(AC::$browsers) );
			$datas .= '<input type="hidden" class="countrychart2" name="OS" value="'.json_encode($os_v).'" capture='."'{$os_capture}'".' />';
			$datas .= '<input type="hidden" class="countrychart2" name="Browser" value="'.json_encode($browsers_v).'" capture='."'{$browsers_capture}'".' />';
		}else{
			$v = array(rand(1000, 100000));
			$capture = json_encode(array('NONE'));
			$datas .= '<input type="hidden" class="countrychart2 none" name="OS" value="'.json_encode($v).'" capture='."'{$capture}'".' />';
			$datas .= '<input type="hidden" class="countrychart2 none" name="Browser" value="'.json_encode($v).'" capture='."'{$capture}'".' />';
		}
		return " <dd><center>{$datas}</center></dd>";
	}
    protected function __renderHead()
    {
		$filters = array('dateby', 'startmonth', 'startyear', 'endmonth', 'endyear', 'startdate', 'enddate', 'country', 'os_browser');
		foreach($_GET as $k=>$v){
			if(in_array($k, $filters)){
				$$k = $v;
			}
		}
		$startmonth = str_pad($startmonth, 2, 0, STR_PAD_LEFT);
		$endmonth = str_pad($endmonth, 2, 0, STR_PAD_LEFT);
		$curr_datetime = date("Y-m-d H:i:s");
		
		$str_date = '';
		if($dateby == 'date'){
			$str_date = "{$startdate} - {$enddate}";
		}else{
			$str_date = "{$startyear} - {$startmonth} to {$endyear} - {$endmonth}";
		}
		
		$str_country = transCountry($country);
		
		$country = json_decode($country, true);
		$country_html = '';
		if(empty($os_browser)){
			foreach($country as $k=>$item){
				$country_html .= "<dl><dt><center>{$k}</center></dt>";
				if($item == 'ALL'){
					$country_html .= $this->getItemHTML($item);
				}else{
					foreach($item as $a){
						foreach($a as $k2=>$b){
							$country_html .= "<dd style='padding:5px;'><center><b>{$b} ({$k2})</b></center></dd>";
							$country_html .= $this->getItemHTML($b);
						}
					}
				}
				$country_html .= "</dl>";
			}
		}else{
			$os_browser = explode("\n", $os_browser);
			$os_browsers = array();
			foreach($os_browser as $v){
				$tmp = explode('   ', $v);
				$os_browsers[$tmp[0]][] = $tmp[1];
			}
			foreach($country as $k=>$item){
				$country_html .= "<dl><dt><center>{$k}</center></dt>";
				if($item == 'ALL'){
					$country_html .= $this->getItemHTML2($item, $os_browsers);
				}else{
					foreach($item as $a){
						foreach($a as $k2=>$b){
							$country_html .= "<dd style='padding:5px;'><center><b>{$b} ({$k2})</b></center></dd>";
							$country_html .= $this->getItemHTML2($b, $os_browsers);
						}
					}
				}
				$country_html .= "</dl>";
			}
		}
		
		$dateby = ucwords( strtolower($dateby));
		

		$this->contents = <<<HTML
	<div id="result" class="os_browser_result">
		<div class="hd">
			<div class="utility">
				<a href="#" class="print">Print</a> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				Export to <a href="#">HTML</a>
			</div>
			<hr />
			<h1>OS And Browser Report</h1>
			<div class="time">{$curr_datetime}</div><br />
			<div><b>By {$dateby}:</b> {$str_date}</div>
			<table class="nob"><tr><td valign="top" width="100"><b class="tit">Region/Country:</b>&nbsp;</td><td>{$str_country}</td></tr></table>
			
		</div>
		<div class="bd">
 {$country_html}
		</div>
	</div>
HTML;
    }
	private function getItemHTML2($item, $os_browsers){
	$html = '';
	//print_r($os_browsers);
	foreach($os_browsers as $os=>$browsers){
		$html .= '<tr><td colspan="5" class="weight">'.$os.'</td> </tr>';
		foreach($browsers as $k=>$browser){
			$html .='<tr>'.($k==0 ? '<td rowspan="'.(count($browsers)+1).'">&nbsp;</td>' : '').'<td>'.$browser.'</td><td class="num">9,999,999</td><td class="num">99%</td><td class="num">&nbsp;</td></tr>';
		}
		$html .= '<tr><td class="total"><strong>Total</strong></td><td class="num"><strong>9,999,999</strong></td><td class="num"><strong>99%</strong></td><td class="num">&nbsp;</td></tr>';
	}
return <<<HTML
<table border="0" cellpadding="0" cellspacing="0">
  <tbody>
  <tr class="weight">
    <td width="194" bgcolor="#cccccc">OS</td>
    <td width="195" bgcolor="#cccccc">Browser</td>

    <td width="117" bgcolor="#cccccc" class="num">Hits</td>
    <td width="95" bgcolor="#cccccc" class="num">Percent</td>
    <td width="49" bgcolor="#cccccc" class="num">&nbsp;</td>
  </tr>
  {$html}
  
</tbody></table>

HTML;
	}
}
?>